﻿Imports MySql.Data.MySqlClient

Public Class DatabaseIO
    Private Shared m_ConnectString = "Server=localhost;Database=junelive;User ID=root;Password="

    Public Shared Function Query(sqlQuery As String) As List(Of Dictionary(Of String, Object))
        Return New MySQL(m_ConnectString).Query(sqlQuery)
    End Function

    Public Shared Sub QueryAsync(sqlQuery As String, Optional callback As Action(Of List(Of Dictionary(Of String, Object))) = Nothing)
        Dim a = New MySQL(m_ConnectString)
        a.QueryAsync(sqlQuery, callback)
    End Sub

    <DebuggerHidden()>
    Public Shared Function GetUtilisateurPrecoParTempid(tempid As String) As UtilisateurPreco
        Dim result As UtilisateurPreco = Nothing
        tempid = tempid.Replace("'", "")

        Debug.Print("tempid=[" & tempid & "]")

        Dim results = Query("SELECT * FROM tempids WHERE tempid='" & tempid & "'")

        For Each tresult In results
            Try
                Console.WriteLine("results(pseudo)=[" & tresult("pseudo") & "], " &
                   "(userlevel)=[" & Val(tresult("userlevel")) & "], " &
                   "(age)=[" & Val(tresult("age")) & "], " &
                   "(sexe)=[" & Val(tresult("sexe")) & "], " &
                   "(ville)=[" & tresult("ville") & "]")

                result = New UtilisateurPreco(
                    tresult("pseudo"),
                    tresult("pseudo"),
                    Val(tresult("userlevel")),
                    Val(tresult("age")),
                    Val(tresult("sexe")),
                    tresult("ville"),
                    Val(tresult("domain"))
                )
            Catch ex As Exception
                Console.WriteLine("results.Read ERROR : " & ex.Message)
            End Try

            Exit For
        Next

        If Not IsNothing(result) Then
            QueryAsync("DELETE FROM tempids WHERE permanent=0 and tempid='" & tempid & "'")
        End If

        Return result
    End Function
End Class
